﻿// -----------------------------------------------------------------------------
// <copyright file="Extensions.cs" company="Justin Wallace Saunders">
//     Copyright (c) Microsoft Public License. All rights reserved.
// </copyright>
// <author name="Justin Wallace Saunders" email="justinsa@live.ca" />
// -----------------------------------------------------------------------------
namespace Community.Tools.Assembly.Configuration.Loader
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Text;

    /// <summary>
    /// StringExtensions class.
    /// </summary>
    public static class Extensions
    {
        /// <summary>
        /// Splits a string on the ';' character and preserves empty entries.
        /// </summary>
        /// <param name="input">The input string</param>
        /// <returns>The array of strings from the split; an empty array if the input is empty, null, or whitespace</returns>
        public static IList<string> SplitOnSemicolon(this string input)
        {
            return input.SplitOnSemicolon(StringSplitOptions.None);
        }

        /// <summary>
        /// Splits a string on the ';' character.
        /// </summary>
        /// <param name="input">The input string</param>
        /// <param name="options">The split option</param>
        /// <returns>The array of strings from the split; an empty array if the input is empty, null, or whitespace</returns>
        public static IList<string> SplitOnSemicolon(this string input, StringSplitOptions options)
        {
            List<string> collection = new List<string>();
            if (!string.IsNullOrWhiteSpace(input))
            {
                collection.AddRange(input.Split(new[] { ';' }, options));
            }

            return collection;
        }

        /// <summary>
        /// Collapses a collection of strings by semi-colon concatenation.
        /// </summary>
        /// <param name="collection">The input array to collapse</param>
        /// <returns>A semi-colon joined string</returns>
        /// <remarks>Removes all empty or whitespace entries in the collection and trims all whitespace and semi-colons</remarks>
        public static string CollapseBySemicolon(this IEnumerable<string> collection)
        {
            if (collection == null)
            {
                return string.Empty;
            }

            StringBuilder output = new StringBuilder();
            foreach (string item in collection)
            {
                string trimmedItem = item.Trim(new[] { ' ', ';' });
                if (!string.IsNullOrWhiteSpace(trimmedItem))
                {
                    output.AppendFormat("{0};", trimmedItem);
                }
            }

            return output.Length == 0 ? string.Empty : output.ToString().TrimEnd(';');
        }

        /// <summary>
        /// Collapses a collection of longs by semi-colon concatenation.
        /// </summary>
        /// <param name="collection">The input array to collapse</param>
        /// <returns>A semi-colon joined string</returns>
        public static string CollapseBySemicolon(this IEnumerable<long> collection)
        {
            if (collection == null)
            {
                return string.Empty;
            }

            StringBuilder output = new StringBuilder();
            foreach (long item in collection)
            {
                output.AppendFormat("{0};", item.ToString(CultureInfo.InvariantCulture));
            }

            return output.Length == 0 ? string.Empty : output.ToString().TrimEnd(';');
        }
    }
}
